package com.xtecher.app.column;

import com.xtecher.app.Config;
import com.xtecher.app.okhttp.IRequestManager;
import com.xtecher.app.okhttp.OnLoadListener;
import com.xtecher.app.okhttp.RequestFactory;

/**
 * Created by Luhao on 2017/2/22.
 */
public class ColumnPresenter implements ColumnContact.Presenter {

    private final IRequestManager manager;
    private ColumnContact.View mView;

    public ColumnPresenter(ColumnContact.View view) {
        mView = view;
        manager = RequestFactory.getRequestManager();
    }

    /**
     * 在此方法判断网络是否可用和界面对应初始化情况
     */
    @Override
    public void start() throws Exception {

    }

    @Override
    public void getColumn(String url) {
        if (mView != null) {
            mView.showLoading("");
        }
        manager.get(Config.GETCOLUM + url, new OnLoadListener() {
            @Override
            public void onSuccess(Object o) {
                if (mView != null) {
                    mView.setData(o, 0);
                    mView.hideLoading();
                }
            }

            @Override
            public void onFailed(String msg) {
                if (mView != null) {
                    mView.hideLoading();
                    mView.onError(msg);
                    mView.showMessage(msg);
                }
            }
        });
    }

    @Override
    public void getColumnList(String url) {
        if (mView != null) {
            mView.showLoading("");
        }
        manager.get(Config.GETCOLUMNLIST + url, new OnLoadListener() {
            @Override
            public void onSuccess(Object o) {
                if (mView != null) {
                    mView.setData(o, 0);
                    mView.hideLoading();
                }
            }

            @Override
            public void onFailed(String msg) {
                if (mView != null) {
                    mView.hideLoading();
                    mView.onError(msg);
                    mView.showMessage(msg);
                }
            }
        });
    }

    @Override
    public void getArticle(String url) {
        if (mView != null) {
            mView.showLoading("");
        }
        manager.get(Config.GETAIRTICLE + url, new OnLoadListener() {
            @Override
            public void onSuccess(Object o) {
                if (mView != null) {
                    mView.setData(o, 0);
                    mView.hideLoading();
                }
            }

            @Override
            public void onFailed(String msg) {
                if (mView != null) {
                    mView.hideLoading();
                    mView.onError(msg);
                    mView.showMessage(msg);
                }
            }
        });
    }

    @Override
    public void getArticleList(String url) {
        if (mView != null) {
            mView.showLoading("");
        }
        manager.get(Config.GETAIRTICLELISTBTID + url, new OnLoadListener() {
            @Override
            public void onSuccess(Object o) {
                if (mView != null) {
                    mView.setData(o, 0);
                    mView.hideLoading();
                }
            }

            @Override
            public void onFailed(String msg) {
                if (mView != null) {
                    mView.hideLoading();
                    mView.onError(msg);
                    mView.showMessage(msg);
                }
            }
        });
    }

    @Override
    public void getBanner() {
        manager.get(Config.GETBANNERBYAPP, new OnLoadListener() {
            @Override
            public void onSuccess(Object o) {
                if (mView != null) {
                    mView.setData(o, 1);
                    mView.hideLoading();
                }
            }

            @Override
            public void onFailed(String msg) {
                if (mView != null) {
                    mView.hideLoading();
                    mView.onError(msg);
                    mView.showMessage(msg);
                }
            }
        });
    }

    @Override
    public void getSplash() {
        manager.get(Config.GETSPLASH, new OnLoadListener() {
            @Override
            public void onSuccess(Object o) {
                if (mView != null) {
                    mView.setData(o, 1);
                    mView.hideLoading();
                }
            }

            @Override
            public void onFailed(String msg) {
                if (mView != null) {
                    mView.hideLoading();
                    mView.onError("splash");
                }
            }
        });
    }

    @Override
    public void checkUpdate(String url) {
        manager.get(Config.CHECKUPDATE, new OnLoadListener() {
            @Override
            public void onSuccess(Object o) {
                if (mView != null) {
                    mView.setData(o, 2);
                    mView.hideLoading();
                }
            }

            @Override
            public void onFailed(String msg) {
                if (mView != null) {
                    mView.hideLoading();
                    mView.onError("splash");
                }
            }
        });
    }
}
